﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_News_AddNew : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnCancel.Attributes.Add("onclick", "return ClearAll()");

        if (!this.IsPostBack)
        {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        }
    }

    private Boolean CheckNick(String strNick, DamiWebDataContext db)
    {
        Boolean bBool = false;
        var query = from p in db.tbl_Nick_Yahoos
                    where p.Nickname == strNick
                    select p;
        if (query.Count<tbl_Nick_Yahoo>() > 0)
            bBool = true;
        return bBool;
    }

    private void AddNew()
    {
        String strMess = "";
        int id = 0;
        try
        {
            WebData db = new WebData();
            DataLibrary.tbl_Category item = new DataLibrary.tbl_Category();
            item.Description = txtDescription.Text;
            item.OrderId = Convert.ToInt32(txtorderId.Text);
            item.ShowTop = chkShowTop.Checked;
            int i = db.tbl_Categorys.InsertOnSubmit(item);
            strMess = i == -1 ? Message.Insert_err : Message.Insert;
            item = db.tbl_Categorys.GetAll()[0];
            id = item.IdCat;
            DataLibrary.tbl_Category_Lang itemLang = new DataLibrary.tbl_Category_Lang();
            itemLang.IdCat = id;
            itemLang.IdCatLang = 1;
            itemLang.IdLang = 1;
            itemLang.NameCat = txtDescription.Text;
            itemLang.Url = txtUrl.Text;

            db.tbl_Category_Langs.InsertOnSubmit(itemLang);

        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }



        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        Response.Redirect(ViewState["URLReferrer"].ToString());

    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
    }

}